/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Nhang Con
 */
@ManagedBean
@SessionScoped
public class Uploadbean {

    private String uploadTo = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/admin/images/upload") + "/";

    /** Creates a new instance of Uploadbean */
    public Uploadbean() {
    }

    public void fileUpload(FileUploadEvent event) {
        try {
            FileOutputStream fos = null;
            UploadedFile file = event.getFile();
            byte[] buff = file.getContents();
            File fil = new File(uploadTo + file.getFileName());
            if (!fil.exists()) {
                try {
                    fil.createNewFile();
                } catch (IOException ex) {
                    Logger.getLogger(Uploadbean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            fos = new FileOutputStream(fil);
            fos.write(buff);
            fos.close();
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("sessionPathFile", file.getFileName());

        } catch (IOException ex) {
            Logger.getLogger(Uploadbean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fileUpload1(FileUploadEvent event) {
        try {
            FileOutputStream fos = null;
            UploadedFile file = event.getFile();
            byte[] buff = file.getContents();
            File fil = new File(uploadTo + file.getFileName());
            if (!fil.exists()) {
                try {
                    fil.createNewFile();
                } catch (IOException ex) {
                    Logger.getLogger(Uploadbean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            fos = new FileOutputStream(fil);
            fos.write(buff);
            fos.close();
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("sessionFile", file.getFileName());
        } catch (IOException ex) {
            Logger.getLogger(Uploadbean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fileUpload2(FileUploadEvent event) {
        try {
            FileOutputStream fos = null;
            UploadedFile file = event.getFile();
            byte[] buff = file.getContents();
            File fil = new File(uploadTo + file.getFileName());
            if (!fil.exists()) {
                try {
                    fil.createNewFile();
                } catch (IOException ex) {
                    Logger.getLogger(CarDetailBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            fos = new FileOutputStream(fil);
            fos.write(buff);
            fos.close();
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("sessionFile2", file.getFileName());
        } catch (IOException ex) {
            Logger.getLogger(CarDetailBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fileUpload3(FileUploadEvent event) {
        try {
            FileOutputStream fos = null;
            UploadedFile file = event.getFile();
            byte[] buff = file.getContents();
            File fil = new File(uploadTo + file.getFileName());
            if (!fil.exists()) {
                try {
                    fil.createNewFile();
                } catch (IOException ex) {
                    Logger.getLogger(CarDetailBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            fos = new FileOutputStream(fil);
            fos.write(buff);
            fos.close();
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("sessionFile3", file.getFileName());
        } catch (IOException ex) {
            Logger.getLogger(CarDetailBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
